kvm虚拟化
=========================================

一：安装
1.1：前提
    1：确保cpu支持hvm
        ~]# grep -E --color=auto "(vmx|svm)" /proc/cpuinfo

    2：显示内核模块信息
        ~]# modprobe kvm  # 载入模块
        ~]# modinfo kvm     # 显示模块

    3：显示Linux内核模块状态
        ~]# lsmod kvm

    备注：学习使用 vmware 虚拟机时需要打开
        ]# 设置 -->  处理器 --> 虚拟化引擎  --> 
            1：首选模式 
                默认自动即可

            2：虚拟化 Inter VT-x/EPT 或 AMD-V/RVI(V)
                勾选此选项

1.2：安装
    ~]# yum -y install kvm qemu-kvm    qemu-kvm-tools   
    ~]# yum -y install libvirt virt-manager virt-install virt-viewer

qemu-img

1.3：启动
    ~]# systemctl start libvirtd.service



 ~]# ifconfig 增加了一个 桥设备  提供 net网络
    virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
            inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
            ether 52:54:00:84:93:3b  txqueuelen 0  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


连接：
    ~]# ssh -X root@172.18.21.70
    ~]# virt-manager
    ~]# rpm -ql libvirt-daemon

    


virsh help


~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=1fa663f2-26fe-4ca9-bebd-95afe34ae10f
DEVICE=eno16777736
ONBOOT=yes
PREFIX=16
BRIDGE="br0"


~]# cat /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
UUID=1fa663f2-26fe-4ca9-bebd-95afe34ae10f
ONBOOT=yes
DNS1=172.18.0.1
IPADDR=172.18.21.70
PREFIX=16
GATEWAY=172.18.0.1


[root@node1 ~]# virsh iface-list
 Name                 State      MAC Address
---------------------------------------------------
 eno16777736          active     00:0c:29:fb:47:47
 lo                   active     00:00:00:00:00:00

~]# virsh help iface-bridge

关闭 Networkmanager 
~]# systemctl stop NetworkManager.service
~]# systemctl disable NetworkManager.service

确保 network 正常运行
~]# systemctl status network.service

~]# virsh iface-bridge eno16777736 br0
[root@node1 ~]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.21.70  netmask 255.255.0.0  broadcast 172.18.255.255
        inet6 fe80::20c:29ff:fefb:4747  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:fb:47:47  txqueuelen 0  (Ethernet)
        RX packets 170  bytes 15829 (15.4 KiB)
        RX errors 0  dropped 2  overruns 0  frame 0
        TX packets 36  bytes 4847 (4.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:fb:47:47  txqueuelen 1000  (Ethernet)
        RX packets 16098  bytes 2604314 (2.4 MiB)
        RX errors 0  dropped 9  overruns 0  frame 0
        TX packets 1318  bytes 259322 (253.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


创建虚拟机的时候就可以桥接网络

virsh help domain

虚拟机示例
~]# virsh list
~]# virsh help list

获取帮助：
    ~]# virsh
    virsh # help
~]# virsh dumpxml rhel6.6

list 列出域
dumpxml：导出指定域的xml配置文件格式
dominfo：
console：登录控制台  ctrl + ]  退出
create：创建并启动域
define：创建域
domid：获取域id
domuuid：获取域的uuid
dominfo：域信息
reboot：重启域
destory：关闭域
shutdown：关闭域
undefine：删除域
save：保存状态至某文件中
restore：从保存文件中恢复域状态
pause：暂停域
resume：恢复暂停域

管理域资源
setmem：改变内存大小，不能超出最大值，只能增加不能减小。单位是kb可以使用 m 单位
setmaxmem：设定内存最大值
setvcpus：修改域的vcpu数量
vcpuinfo：获取域的信息
vcpupin：cpu 绑定
domblkstat：
domifstat：域的接口列表
domiflist：域的接口列表





动态增删 disk
    qemu-img create
    virsh attach-disk
    virsh detach-disk

动态删除 interface
    virsh attach-interface <domain> <type> <source>
        type：bridge
        source：某桥的实例
    virsh detach-interface <domian> <type> --mac MAC


~]# qemu-img create -f qcow2 -o ? /tmp/test.qcow2
Supported options:
size             Virtual disk size
compat           Compatibility level (0.10 or 1.1)
backing_file     File name of a base image
backing_fmt      Image format of the base image
encryption       Encrypt the image
cluster_size     qcow2 cluster size
preallocation    Preallocation mode (allowed values: off, metadata, falloc, full)
lazy_refcounts   Postpone refcount updates

~]# qemu-img create -f qcow2 -o preallocation=metadata  /tmp/test.qcow2 120G
[root@node1 ~]# ls -lh /tmp/test.qcow2 
-rw-r--r-- 1 root root 121G Jun 19 14:26 /tmp/test.qcow2
[root@node1 ~]# du -sh /tmp/test.qcow2
23M /tmp/test.qcow2

[root@node1 ~]# qemu-img resize /tmp/test.qcow2 130G
Image resized.

~]# qemu-img create -f qcow2 -o preallocation=metadata /tmp/test.img 20G

备注：换算单位不同，
    虚拟机使用 1000 为单位
    操作系统使用 1024 位单位

~]# virsh help attach-disk
添加磁盘
~]# virsh attach-disk rhel6.6 /tmp/test.img
~}# virsh detach-disk rhel6.6 vdb

创建一对网卡：
[root@node1 ~]# ip link add veth1.0 type veth peer name veth1.1
[root@node1 ~]# ip link show
8: veth1.1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
    link/ether 0a:56:01:f9:55:81 brd ff:ff:ff:ff:ff:ff
9: veth1.0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
    link/ether c6:b0:62:7e:14:75 brd ff:ff:ff:ff:ff:ff


~]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.000c29fb4747   yes     eno16777736
virbr0      8000.000000000000   yes 


~]# brctl -h
不需要创建，自动完成 
[root@node1 ~]# brctl addif virbr0 veth1.0
[root@node1 ~]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.000c29fb4747   yes     eno16777736
virbr0      8000.c6b0627e1475   yes     veth1.0

~]# virsh help attach-interface

 ~]# virsh attach-interface rhel6.6 bridge verbr0

~]# virsh attach-interface rhel6.6 bridge virbr0

~]# virsh help detach-interface 

virsh detach-intarface rhel6.6 --mac 52:54:00:4e:ca:4e   
    --mac 地址是 ~]# virsh domiflist rhel6.6 显示的mac地址




虚拟机创建安装工具：virt-install
    一定要带上选项：--input tablet ：解决鼠标不一致问题。



qemu-kvm 虚拟机管理，创建删除等。

[root@node1 ~]# rpm -ql qemu-kvm
/usr/libexec/qemu-kvm

[root@node1 ~]# ln -sv /usr/libexec/qemu-kvm /sbin/
‘/sbin/qemu-kvm’ -> ‘/usr/libexec/qemu-kvm’

[root@node1 ~]# qemu-kvm -net nic,model=?
qemu: Supported NIC models: ne2k_pci,i82551,i82557b,i82559er,rtl8139,e1000,pcnet,virtio

[root@node1 cirros]# qemu-img info cirros-no_cloud-0.3.0-x86_64-disk.img 
image: cirros-no_cloud-0.3.0-x86_64-disk.img
file format: qcow2
virtual size: 39M (41126400 bytes)
disk size: 11M
cluster_size: 65536
Format specific information:
    compat: 0.10

cp cirros-no_cloud-0.3.0-x86_64-disk.img ./vms/test/test.qcow2
























































yum -y groupinstall "GNOME Desktop"

http://jingyan.baidu.com/article/bea41d439628c2b4c51be62b.html
https://developer.gnome.org/gtk3/stable/GtkStyleProvider.html




kvm模块载入后系统的运行模式
    内核模式：
    用户模式：
    来宾模式：

ept rvi



安装
~]# 
    Virtualization

    前提：
        1：确保cpu支持hvm
        2：内核模块
            modinfo
             modprobe kvm
             lsmod | grep kvm


        python-virtinst
        ~]# yum -y install libvirt virt-manager 

        ~]# systemctl start libvirtd.service
            net 网络

        ssh -X root@172.18.21.67   图形界面

~]# virsh list
~]# virsh console rhel6.6
~]# yum -y install qemu-img
~]# qemu-img create -f qcow2 -o ? /tmp/test.qcow2
virsh attach-disk rhel6.6 /tmp/test.img vdb

~]# ip link add name veth1.0 type veth peer name veth1.1

~]# brctl show 

virsh attach-interface rehl
~]# yum -y install virt-install

cat /proc/34039/cmdline



~]# cirros


yum -y install tigervnc

~]# ip netns







































































